package com.yunshang.shoppingmall.ShopTask;


import com.yunshang.shoppingmall.ShopTask.Task.Hsys.HsysOrder;

import com.yunshang.shoppingmall.Methods.ShopTaskMethods;
import com.yunshang.shoppingmall.ShopTask.Task.Hsys.HsysUser;
import com.yunshang.shoppingmall.common.UserRequest;
import com.yunshang.shoppingmall.entity.Config;
import com.yunshang.shoppingmall.entity.ShopTask;
import com.yunshang.shoppingmall.entity.ShopTaskState;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
/**
 * 结算任务
 */
public class ShopTaskRun {

    //执行时间
    //当前时间
    Date lbTime;
    //任务数据时间
    Date date1;
    //当前时间
    Date date2;

    //执行控制
    boolean blTaskJc = false;

    @Scheduled(fixedRate=60000)
    public void fixedDelay() {
        String ip = "";//0:0:0:0:0:0:0:1
        if (!ip.equals("0:0:0:0:0:0:0:1")) {
            if (!ShopTaskState.isStart()) {
                //System.out.println("A:");
                lbTime = new Date();
                date1 = new Date();
                date2 = new Date();
                //如果没有任务在运行
                if (!ShopTaskState.isComplete() && !blTaskJc) {
                    int minutes = (new Date()).getMinutes();
                    System.out.println("MM:" + minutes);
                    //如果是每10分钟的时候，检查是否执行任务
                    if (minutes == 0 || minutes == 10 || minutes == 20 || minutes == 30 || minutes == 40 || minutes == 50) {
                        blTaskJc = true;
                        System.out.println("读取任务");
                        ThroughTask();
                    }
                }
            }
        }
    }

    public void ThroughTask() {

        List<ShopTask> listShopTask = ShopTaskMethods.getListShopTask();
        System.out.println("任务数量："+listShopTask.size());

        for(int i=0;i<listShopTask.size();i++) {
            System.out.println("任务判断："+i);
            ShopTask shopTask = listShopTask.get(i);
            //排除时段
            boolean bl = true;
            if (shopTask.getTaskRuleOut1() != shopTask.getTaskRuleOut2()) {
                if ((new Date()).getHours() >= shopTask.getTaskRuleOut1() && (new Date()).getHours() <= shopTask.getTaskRuleOut2()) {
                    bl = false;
                }
            }

            if (bl) {

                //如果是按分结算
                if (shopTask.getTaskCategory() == 1)
                {
                    if (shopTask.getLastTime() == null)
                    {
                        System.out.println("时间null");
                        AutoTask(shopTask.getId(),shopTask.getTasks(), i, "yyyy-MM-dd HH:mm:ss",shopTask);
                        break;
                    }
                    else
                    {
                        try
                        {
                            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date dNow = new Date();
                            Calendar c = Calendar.getInstance();
                            c.setTime(shopTask.getLastTime());
                            c.add(Calendar.MINUTE,shopTask.getTaskTime());
                            //如果已经超过间隔时间就执行任务
                            System.out.println("时间:"+c.getTime().toString()+"---"+dNow.getTime());
                            if ((dNow.getTime()-c.getTime().getTime()) >= 0)
                            {
                                AutoTask(shopTask.getId(),shopTask.getTasks(), i, "yyyy-MM-dd HH:mm:ss",shopTask);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }

            }
        }

        blTaskJc = false;
    }

    public void AutoTask(int id,String tasks,int i,String DateFormat,ShopTask shopTask1) {
        ShopTaskState.setComplete(true);
        System.out.println("tasks:"+tasks+"---I:"+i+"---DateFormat:"+DateFormat);

        //更新执行时间
        ShopTask shopTask = new ShopTask();
        shopTask.setId(id);
        shopTask.setLastTime(new Date());
        shopTask1.setLastTime(new Date());
        int rows= ShopTaskMethods.updateByPrimaryKeySelective(shopTask);

        //执行任务
        switch (tasks)
        {
            case "201"://荟生用户同步
                HsysUser hsysUser = new HsysUser();
                hsysUser.getUser();
                break;
            case "202"://荟生订单同步
                //HsysOrder hsysOrder = new HsysOrder();
                //hsysOrder.getOrder();
                break;
            default:
                break;
        }
    }
}